<?php
namespace Home\Event;

use Think\Controller;

class PeopleEvent extends Controller
{
    public function Attend()
    {
        $id = isset($_GET['uid']) && !empty($_GET['uid'])?$_GET['uid']:session('home.id');
        $data['id'] = $id;
        // $order['datetime'] = 'DESC';
        //查询问题表
        $user_id['user_id'] = $id;
        $attend_id['attended_id'] = $id;
        $att = D('user_user')->field('attended_id')->where($user_id)->select();//查寻观看关注了的ids
        $atted = D('user_user')->field('user_id')->where($attend_id)->select();//查询观看关注者的ids
        $attend = array();
        $attended = array();
        //将关注了的ids进行数组整理
        foreach ($att as $key => $val) {
            $attend[] = $val['attended_id'];
        }
        $attendids = $attend;
        //将关注者的ids进行数组整理
        foreach ($atted as $key => $val) {
            $attended[] = $val['user_id'];
        }
        $attendedids = $attended;
        //登录用户关注和被关注的ids 只传递登录用户的这些ids
        if ($id != session('home.id')) {
            $user_id['user_id'] = session('home.id');
            $attend_id['attended_id'] = session('home.id');
            $atthome = D('user_user')->field('attended_id')->where($user_id)->select();
            $attedhome = D('user_user')->field('user_id')->where($attend_id)->select();
            foreach ($atthome as $key => $val) {
                $attendhome[] = $val['attended_id'];
            }
            $attendids = $attendhome;
            foreach ($atted as $key => $val) {
                $attendedhome[] = $val['user_id'];
            }
            $attendedids = $attendedhome;
        }
        //判断是否为空 进行查询用户信息
        if (empty($attend)) {
            $attend = null;
        } else {
            $ids = implode(',',$attend);
            $attend = D('user')->field('name,id,image')->where("id in ($ids)")->select();
        }
        if (empty($attended)) {
            $attended = null;
        } else {
            $ids = implode(',',$attended);
            $attended = D('user')->field('name,id,image')->where("id in ($ids)")->select();
        }
        $attends['attended'] = $attended;
        $attends['attend'] = $attend;
        $attends['attendids'] = $attendids;
        $attends['attendedids'] = $attendedids;
        // V($attends);
        //传递模版信息
        return $attends;
    }

    public function User()
    {
        $id = session('home.id');
        $userinfo = D('userinfo')->getById($id);
        $user = D('user')->getById($id);
        foreach ($userinfo as $key => $val) {
            $user[$key] = $val;
        }
        return $user;
    }

}